<template>
  <div class="search-input" :class="{'fix-input': isFixed}">
    <a class="icon iconfont icon-sousuo"></a>
    <!-- 输入框输入值时显示搜索提示 -->
    <input type="text" placeholder="搜索" v-model="keyWord"/>
  </div>
</template>

<script>
  import pubsub from 'pubsub-js'
  export default {
    name: 'SearchInput',
    data() {
      return {
        keyWord: '',
        isFixed: null
      }
    },
    watch: {
      // 监听keyWord的值，发生改变就发送新的值给SearchResult
      keyWord(val) {
        this.sendKeyWord(val)
      }
    },
    methods: {
      // 给SearchResult发送keyWord
      sendKeyWord(val) {
        pubsub.publish('getKeyWord', val)
      },
      
      // 接收Header的isFixed
      getIsFixed(isFixed) {
        // console.log('isFixed');
        this.isFixed = isFixed
      }
    },
    mounted() {
      // 事件总线，接收Header传过来的isFixed
      this.$bus.$on('getIsFixed', this.getIsFixed)
    },
    beforeDestroy() {
      // 解绑事件总线
      this.$bus.$off('getIsFixed')
    }
  }
</script>

<style scoped>
  .search-input {
    display: flex;
    align-items: center;
    height: 48px;
    width: 250px;
    border: 2px solid #a2c3d4;
    border-radius: 48px;
    /* background-color: #fff; */
    margin: 0 10px;
    /* background-color: #000; */
  }

  .search-input .icon {
    color: #a2c3d4;
    font-size: 20px;
    font-weight: 400;
    margin-left: 15px;
  }

  .search-input input {
    height: 30px;
    width: 200px;
    line-height: 30px;
    border: 0;
    color: #fff;
    font-size: 16px;
    font-weight: 700;
    background-color: transparent;
    padding-left: 10px;
  }

  .fix-input .icon,
  .fix-input input {
    color: #000;
  }

  ::-webkit-input-placeholder { /* WebKit browsers */
    color: #86a3b1;
    font-size: 15px;
  }

  ::-moz-placeholder { /* Mozilla Firefox 19+ */
    color: #86a3b1;
    font-size: 15px;
  }

  :-ms-input-placeholder { /* Internet Explorer 10+ */
    color: #86a3b1;
    font-size: 15px;
  }
</style>